//
//  CoursesDetaileFirstCell.m
//  Menu
//
//  Created by mc on 16/4/26.
//  Copyright © 2016年 xmj. All rights reserved.
//

#import "CoursesDetaileFirstCell.h"
#import "View+MASAdditions.h"
#import "SelectCourses.h"
#import <UIImageView+WebCache.h>


/** 标题字体 */
#define kNameFont   [UIFont systemFontOfSize:16]
/** 正文字体 */
#define kTextFont   [UIFont systemFontOfSize:14]
#define IPHONE6_SIZE(X)  ([UIScreen mainScreen].bounds.size.width/(750.0/2.0))*X

@interface CoursesDetaileFirstCell()
@property (nonatomic, strong) UIImageView *pictureView;
@property (nonatomic, strong) UILabel *titleView;
@property (nonatomic, strong) UILabel *menuStyleTextView;
@property (nonatomic, strong) UIImageView *menuStyleImageView;
@property (nonatomic, strong) UILabel *techerTextView;
@property (nonatomic, strong) UILabel *textView;
@property (nonatomic, strong) UILabel *peopleNumView;
@property (nonatomic, strong) UIView *seperateView;
@end

@implementation CoursesDetaileFirstCell


- (UILabel *)textView{
    if (_textView ==nil) {
        _textView = [[UILabel alloc]init];
        _textView.font = kTextFont;
        _textView.numberOfLines = 0;
        NSDictionary *textDict = @{NSFontAttributeName: kTextFont};
        [self.selectCourses.tags boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
        [self.contentView addSubview:_textView];
    }
    return _textView;
}

- (UIImageView *)pictureView{
    if (_pictureView ==nil) {
        _pictureView = [[UIImageView alloc]init];
        [self.contentView addSubview:_pictureView];
    }
    return _pictureView;
}

- (UILabel *)titleView{
    if (_titleView ==nil) {
        _titleView = [[UILabel alloc]init];
        _textView.numberOfLines = 0;
        NSDictionary *textDict = @{NSFontAttributeName: kNameFont};
        [self.selectCourses.title boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:textDict context:nil];
        [self.contentView addSubview:_titleView];
    }
    return _titleView;
}

- (UILabel *)menuStyleTextView{
    if (_menuStyleTextView == nil) {
        _menuStyleTextView = [[UILabel alloc]init];
        _menuStyleTextView.font = kTextFont;
        [self.contentView addSubview:_menuStyleTextView];
    }
    return _menuStyleTextView;
}
- (UIImageView *)menuStyleImageView{
    if (_menuStyleImageView == nil) {
        _menuStyleImageView = [[UIImageView alloc]init];
        [self.contentView addSubview:_menuStyleImageView];
    }
    return _menuStyleImageView;
}

- (UILabel *)techerTextView{
    if (_techerTextView == nil) {
        _techerTextView = [[UILabel alloc]init];
        _techerTextView.font = kTextFont;
        [self.contentView addSubview:_techerTextView];
    }
    return _techerTextView;
}

- (UILabel *)peopleNumView{
    if (_peopleNumView == nil) {
        _peopleNumView = [[UILabel alloc]init];
        _peopleNumView.font = kTextFont;
        [self.contentView addSubview:_peopleNumView];
    }
    return _peopleNumView;
}

- (UIView *)seperateView{
    if (_seperateView == nil) {
        _seperateView = [[UIView alloc]init];
        _seperateView.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_seperateView];
    }
    return _seperateView;
}

- (void)setSelectCourses:(SelectCourses *)selectCourses
{
    _selectCourses = selectCourses;
    
    // 1> 设置数据
    [self settingData];
    
    // 2> 设置位置
    [self settingFrame];
}

- (void)settingData
{
    
    // 正文
    self.textView.text = self.selectCourses.tags;
    [self.pictureView sd_setImageWithURL:[NSURL URLWithString:self.selectCourses.albums]];
    self.titleView.text = self.selectCourses.title;
   // NSString *str = self.selectCourses.tags;
   // NSArray *array = [str componentsSeparatedByString:@";"];
//    for (int i = 0 ; i < array.count; i++) {
//        if (i == 1) {
//            self.menuStyleTextView = array[i];
//            break;
//        }
//        
//    }
    self.menuStyleTextView.text = @"中点";
    self.menuStyleImageView.image = [UIImage imageNamed:@"courseAssign"];
    
    self.techerTextView.text = @"老师：1122";
    self.peopleNumView.text = @"总人数：12人 剩余：0人";
}

- (void)settingFrame
{
    [self.pictureView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.titleView.mas_top).with.offset(-10);
    }];
    
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.right.equalTo(self.contentView.mas_right);
        make.bottom.equalTo(self.peopleNumView.mas_top).with.offset(-8);
        //make.height.equalTo(@20);
    }];
   
    [self.peopleNumView mas_makeConstraints:^(MASConstraintMaker *make) {

        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.right.equalTo(self.contentView.mas_right);
        make.height.equalTo(@20);
    }];
    
    [self.menuStyleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.peopleNumView.mas_bottom).with.offset(8);
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
       // make.right.equalTo(self.menuStyleTextView.mas_left).with.offset(-5);
       // make.bottom.equalTo(self.seperateView.mas_top).with.offset(-10);
        make.height.equalTo(@20);
        make.width.equalTo(@25);
    }];
    [self.menuStyleTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.peopleNumView.mas_bottom).with.offset(8);
       // make.bottom.equalTo(self.seperateView.mas_top).with.offset(-10);
        make.left.equalTo(self.menuStyleImageView.mas_right).with.offset(5);
        make.height.equalTo(@20);
        make.width.equalTo(@100);
    }];
    
    
    [self.seperateView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.menuStyleImageView.mas_bottom).with.offset(10);
        make.left.equalTo(self.contentView.mas_left);
        make.right.equalTo(self.contentView.mas_right);
        make.height.equalTo(@1);
    }];
    [self.techerTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.right.equalTo(self.contentView.mas_right);
        make.top.equalTo(self.seperateView.mas_bottom).with.offset(5);
        make.height.equalTo(@20);
    }];
    [self.textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.contentView.mas_left).with.offset(10);
        make.right.equalTo(self.contentView.mas_right);
        make.top.equalTo(self.techerTextView.mas_bottom).with.offset(8);
        make.bottom.equalTo(self.contentView).with.offset(-5);
    }];
    
    
    
    // [self.textView setContentHuggingPriority:UILayoutPriorityRequired forAxis:UILayoutConstraintAxisVertical];
}

+ (instancetype)coursesDetailFirstCellWithTableView:(UITableView *)tableView
{
    static NSString *ID = @"coursesDetaileFirstCell";
    CoursesDetaileFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[CoursesDetaileFirstCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    return cell;
}

+ (CGFloat)height:(SelectCourses *)selectCourses{
    
    
    CGSize titleSize = [selectCourses.title boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    CGSize textSize = [selectCourses.tags boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil].size;
    // sizeWithFont:(UIFont *)font constrainedToSize:(CGSize)size
    CGFloat cellHeight = IPHONE6_SIZE(180) + titleSize.height + 61 + textSize.height + 55;
    return cellHeight;
}
@end
